package com.scitc.pleasurevideo.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.scitc.pleasurevideo.R;

import java.util.List;

public class CategoryRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private List<String> items;
    private Integer position;

    public CategoryRecyclerViewAdapter(Context context, List<String> items) {
        this.context = context;
        this.items = items;
        this.position = 0;
    }

    public void addAllItems(List<String> items) {
        if (items == null || items.size() < 1) {
            return;
        }
        this.items.clear();
        this.items.addAll(items);
        this.position = 0;
        notifyDataSetChanged();
    }

    public void setPosition(Integer position) {
        if (position < 0 || position > items.size()) {
            this.position = 0;
        } else {
            this.position = position;
        }
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        if (items == null || items.size() < 1) {
            return 0;
        }
        return items.size();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        View view = layoutInflater.inflate(R.layout.item_tag_category, parent, false);
        return new ViewHolder(view);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView title;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.title);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, final int position) {
        final String news = items.get(position);
        if (viewHolder instanceof ViewHolder) {
            ViewHolder holder = (ViewHolder) viewHolder;
            holder.title.setText(news);
            if (this.position == position) {
                holder.title.setTextColor(ContextCompat.getColor(context, R.color.white));
                holder.title.setBackground(ContextCompat.getDrawable(context, R.drawable.bg_tag_category_text_on));
            } else {
                holder.title.setTextColor(ContextCompat.getColor(context, R.color.black));
                holder.title.setBackground(ContextCompat.getDrawable(context, R.drawable.bg_tag_category_text_off));
            }

            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onClickItemListener.onClickItemListener(position);
                }
            });
        }
    }

    public OnClickItemListener onClickItemListener;

    public interface OnClickItemListener {
        void onClickItemListener(int position);
    }

    public void setOnClickItemListener(OnClickItemListener onClickItemListener) {
        this.onClickItemListener = onClickItemListener;
    }
}
